package com.finance.action.admin.user;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.common.vo.JsonVo;
import com.common.vo.PageVo;
import com.finance.entity.Finance;
import com.finance.entity.Loan;
import com.finance.entity.LoanBasic;
import com.finance.entity.User;
import com.finance.entity.YhUser;
import com.finance.entity.customer.CallRecordInfo;
import com.finance.entity.customer.JingdongConsumption;
import com.finance.entity.customer.TaobaoConsumption;
import com.finance.service.FinanceService;
import com.finance.service.LoanBasicService;
import com.finance.service.LoanService;
import com.finance.service.ProductService;
import com.finance.service.YhUserService;
import com.finance.service.customer.CallRecordInfoService;
import com.finance.service.customer.JingdongConsumptionService;
import com.finance.service.customer.TaobaoConsumptionService;
import com.web.utils.DateUtil;
import com.web.utils.FileUtils;
import com.web.utils.UserManagerUtil;

@Controller("adminUserAction")
@RequestMapping("admin/user")
public class UserAction {

	@Resource(name = "YhUserService")
	private YhUserService yhUserService;

	@Resource(name = "adminProductService")
	private ProductService productService;

	@Resource(name = "adminLoanService")
	private LoanService loanService;

	@Resource(name = "adminLoanBasicService")
	private LoanBasicService loanBasicService;

	@Resource(name = "FinanceService")
	private FinanceService financeService;

	/*
	 * /*************************************************************************
	 * *********************客户
	 * ****************************************************
	 * *****************************************
	 */

	/*
	 * 客户列表
	 */
	@RequestMapping("user_list.htm")
	public String user_list(Model model,
			@RequestParam(defaultValue = "1") int pageNum, YhUser search) {
		PageVo<YhUser> page = yhUserService.findPageBySearch(search, pageNum);
		User u = UserManagerUtil.getCurrentUser();
		int z = yhUserService.findByzc(1);
		int wc = yhUserService.findBywc(2);
		int y = yhUserService.findByYuQi(3);
		List<LoanBasic> list = loanBasicService.list();
		int size = list.size();
		if(size != 0){
			double overMoney = loanBasicService.findByOverMoney();
			model.addAttribute("overMoney", overMoney);
		}else {
			double overMoney = 0;
			model.addAttribute("overMoney", overMoney);
		}
		model.addAttribute("page", page);
		model.addAttribute("search", search);
		model.addAttribute("z", z);
		model.addAttribute("wc", wc);
		model.addAttribute("y", y);
		model.addAttribute("u", u);
		return "manage/user/user_list";
	}

	@ResponseBody
	@RequestMapping("ajax_user_list")
	public Map<String, Object> ajax_user_list(
			@RequestParam(defaultValue = "1") int pageNum, YhUser search,
			String o, String n) throws UnsupportedEncodingException {

		search.setOldTime(DateUtil.toDate(o));
		search.setNewTime(DateUtil.toDate(n));
		// *********************************UTF-8乱码处理****************************************************//
		if (StringUtils.isNotEmpty(search.getYhUserName())) {
			String yhUserName = new String(search.getYhUserName().getBytes(
					"iso-8859-1"), "UTF-8");
			search.setYhUserName(yhUserName);
		}
		if (StringUtils.isNotEmpty(search.getUserName())) {
			String userName = new String(search.getUserName().getBytes(
					"iso-8859-1"), "UTF-8");
			search.setUserName(userName);
		}

		// *********************************日期转换***************************************************//
		// Date oldTime = DateUtil.toDate(o);
		// Date newTime = DateUtil.toDate(n);

		PageVo<YhUser> page = yhUserService.findPageBySearch(search, pageNum);
		for (int i = 0; i < page.getList().size(); i++) {
			page.getList()
					.get(i)
					.setCreateTime(
							DateUtil.Date2Stirng(page.getList().get(i)
									.getYhCreationTime()));
		}
		Map<String, Object> map = new HashMap<>();
		map.put("code", 0);
		map.put("msg", 0);
		map.put("count", page.getCount());
		map.put("data", page.getList());
		return map;
	}

	/*
	 * 添加客户
	 */
	@RequestMapping("user_add.htm")
	public String user_add(Model model) {
		User user = UserManagerUtil.getCurrentUser();
		model.addAttribute("user", user);
		return "manage/user/user_add";
	}

	/**
	 * 身份证失焦
	 * 
	 * @param record
	 * @param time
	 * @return
	 */
	@ResponseBody
	@RequestMapping("ajax_user_idCard")
	public JsonVo<YhUser> ajax_user_idCard(String yhUserIdcard) {
		JsonVo<YhUser> json = new JsonVo<YhUser>();
		YhUser user = yhUserService.findByIdCard(yhUserIdcard);
		if (user != null) {
			json.setSuccess(true);
			json.setT(user);
			json.setMsg("此身份证已存在，请勿重复添加！");
		}
		return json;
	}

	@ResponseBody
	@RequestMapping("ajax_user_add")
	public JsonVo<YhUser> ajax_user_add(YhUser record, String time) {
		User user = UserManagerUtil.getCurrentUser();
		JsonVo<YhUser> json = new JsonVo<YhUser>();
		Date a = DateUtil.toDate(time);
		String bri = DateUtil.toStringXiumaDateTime(a);
		Date t = new Date();
		Integer briYear = Integer.parseInt(bri.substring(0, 4));
		Integer briMouth = Integer.parseInt(bri.substring(4, 6));
		Integer briDay = Integer.parseInt(bri.substring(6, 8));
		// long nowTime = t.getTime() - bri.getTime();
		// long d = nowTime/1000/60/60/24;
		String tt = DateUtil.toStringXiumaDateTime(t);
		Integer Year = Integer.parseInt(tt.substring(0, 4));
		Integer mouth = Integer.parseInt(tt.substring(4, 6));
		Integer nowDay = Integer.parseInt(tt.substring(6, 8));
		if (mouth == briMouth) {
			if (nowDay > briDay) {
				Integer day = Year - briYear;
				record.setYhUserId(tt);
				record.setYhUserAge(day);
				record.setUserId(user.getUserId());
				record.setYhCreationTime(new Date());
				record.setUserName(user.getName());
				record.setLoanState(0);
				yhUserService.insert(record);
				json.setSuccess(true);
				json.setMsg("已成功添加客户" + record.getYhUserName());
				System.out.println("-------------------------" + json.getMsg()
						+ "-----------------------------");
			} else {
				Integer day = Year - briYear;
				record.setYhUserId(tt);
				record.setYhUserAge(day - 1);
				record.setUserId(user.getUserId());
				record.setYhCreationTime(new Date());
				record.setUserName(user.getName());
				record.setLoanState(0);
				yhUserService.insert(record);
				json.setSuccess(true);
				json.setMsg("已成功添加客户" + record.getYhUserName());
				System.out.println("-------------------------" + json.getMsg()
						+ "-----------------------------");
			}
		} else if (mouth > briMouth) {
			Integer day = Year - briYear;
			record.setYhUserId(tt);
			record.setYhUserAge(day);
			record.setUserId(user.getUserId());
			record.setUserName(user.getName());
			record.setYhCreationTime(new Date());
			record.setLoanState(0);
			yhUserService.insert(record);
			json.setSuccess(true);
			json.setMsg("已成功添加客户" + record.getYhUserName());
			System.out.println("-------------------------" + json.getMsg()
					+ "-----------------------------");
		} else if (mouth < briMouth) {
			Integer day = Year - briYear;
			record.setYhUserId(tt);
			record.setYhUserAge(day - 1);
			record.setUserId(user.getUserId());
			record.setYhCreationTime(new Date());
			record.setUserName(user.getName());
			record.setLoanState(0);
			yhUserService.insert(record);
			json.setSuccess(true);
			json.setMsg("已成功添加客户" + record.getYhUserName());
			System.out.println("-------------------------" + json.getMsg()
					+ "-----------------------------");
		}
		return json;
	}

	/*
	 * 客户基本信息
	 */
	@RequestMapping("user_info.htm")
	public String user_info(Model model, String yhUserId) {
		YhUser user = yhUserService.selectByPrimaryKey(yhUserId);
		model.addAttribute("user", user);
		return "manage/user/user_info";
	}

	/*
	 * 客户贷款
	 */
	@RequestMapping("loan.htm")
	public String loan(Model model, String yhUserId) {
		YhUser yhuser = yhUserService.selectByPrimaryKey(yhUserId);
		model.addAttribute("yhuser", yhuser);
		return "manage/user/loan";
	}

	/**
	 * 先息后本
	 * 
	 * @param loan
	 * @return
	 */
	@ResponseBody
	@RequestMapping("ajax_add_loan_xxhb")
	public JsonVo<String> ajax_add_loan_xxhb(Loan loan, LoanBasic loanBasic,
			String str, String sto, double interest, YhUser yhUser,Finance finance)
			throws ParseException {
		User user = UserManagerUtil.getCurrentUser();
		loan.setStartTime(DateUtil.toDate(str));
		loan.setStopTime(DateUtil.toDate(sto));
		JsonVo<String> json = new JsonVo<String>();
		// 先息后本 日息
		if (loan.getLoanType() == Loan.TYPE_DAY) {
			// 借款天数
			long time = loan.getStopTime().getTime()
					- loan.getStartTime().getTime();
			long day = time / 1000 / 60 / 60 / 24;
			// 实际到手金额
			double a = (double) day;
			double ad = loan.getLoanMoney() - loan.getHomeVisitsMoney()
					- loan.getIntermediary() - loan.getBond();
			loan.setActualMoney(ad);

			// 实际还款金额
			double sm = loan.getLoanMoney()
					+ (loan.getLoanMoney() * day * 0.01);
			loan.setStillMoney(sm);

			// 实际收款金额
			double cm = sm - loan.getBond();
			loan.setCollectMoney(cm);

			Date t = new Date();
			String tt = DateUtil.toStringXiumaDateTime(t);
			loan.setLoanType(Loan.TYPE_DAY);
			loan.setUserName(user.getName());
			loan.setUserId(user.getUserId());
			loan.setProductName("先息后本");
			loan.setLoanId(tt);
			loan.setCreateTime(new Date());
			loan.setAlreadyRepaid(0);
			loan.setNotYet(loan.getStillMoney());
			loanService.add(loan);
			finance.setFinanceId(tt);
			finance.setYhUserName(loan.getTyUserName());
			finance.setYhUserId(loan.getTyUserId());
			finance.setUserId(user.getUserId());
			finance.setUserName(user.getName());
			finance.setLoanMoney(loan.getLoanMoney());
			finance.setContractMoney(loan.getContractMoney());
			finance.setStartTime(loan.getStartTime());
			finance.setStopTime(loan.getStopTime());
			finance.setHomeVisitsMoney(loan.getHomeVisitsMoney());
			finance.setIntermediary(loan.getIntermediary());
			finance.setBond(loan.getBond());
			finance.setActualMoney(loan.getActualMoney());
			finance.setStillMoney(loan.getStillMoney());
			finance.setCollectMoney(loan.getCollectMoney());
			loan.setCreateTime(new Date());
			financeService.insert(finance);
			// 每期还款金额
			double stillDay = (double) (a / 5);
			int stillMoney = (int) (day / 5);
			double cc = (double) stillMoney;
			int still = 0;
			if (cc < stillDay) {
				still = stillMoney + 1;
			} else if (cc == stillDay) {
				still = stillMoney;
			}
			double oneMoney = sm / still;
			BigDecimal b = new BigDecimal(oneMoney);
			double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			for (int i = 1; i < still + 1; i++) {
				// 每期还款时间
				SimpleDateFormat dateFormat = new SimpleDateFormat(
						"yyyy年MM月dd日");
				Date date = loan.getStartTime();
				Date date1 = loan.getStopTime();
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				calendar.add(Calendar.DAY_OF_MONTH, +5 * i);
				date = calendar.getTime();
				if (date.getTime() < date1.getTime()) {
					System.out.println("下一次的还款时间： " + dateFormat.format(date)
							+ "第" + i + "期还款，还款金额为" + f1);

					loanBasic.setCreateTime(new Date());
					loanBasic.setLoanBasicId(tt + i);
					loanBasic.setLoanId(loan.getLoanId());
					loanBasic.setStillMoney(f1);
					loanBasic.setStillState(LoanBasic.STILL_STATE_WEIHUAN);
					loanBasic.setStillTime(date);
					loanBasic.setUserId(user.getUserId());
					loanBasic.setUserName(user.getName());
					loanBasic.setYhUserId(loan.getTyUserId());
					YhUser y = yhUserService.selectByPrimaryKey(loan
							.getTyUserId());
					if (y != null
							&& y.getLoanState() == YhUser.LOAN_STATE_WEIDAIKUAN) {
						yhUser.setLoanState(YhUser.LOAN_STATE_ZHENGCHANG);
						yhUser.setYhUserId(loan.getTyUserId());
						yhUserService.updateLoanSate(yhUser);
					}
					loanBasicService.add(loanBasic);
					json.setSuccess(true);
					json.setMsg("已完成借款");
				} else if (date.getTime() > date1.getTime()) {
					System.out.println("下一次的还款时间： " + dateFormat.format(date1)
							+ "第" + i + "期还款，还款金额为" + f1);

					loanBasic.setCreateTime(new Date());
					loanBasic.setLoanBasicId(tt + i);
					loanBasic.setLoanId(loan.getLoanId());
					loanBasic.setStillMoney(f1);
					loanBasic.setStillState(LoanBasic.STILL_STATE_WEIHUAN);
					loanBasic.setStillTime(date1);
					loanBasic.setUserId(user.getUserId());
					loanBasic.setUserName(user.getName());
					loanBasic.setYhUserId(loan.getTyUserId());
					YhUser y = yhUserService.selectByPrimaryKey(loan
							.getTyUserId());
					if (y != null
							&& y.getLoanState() == YhUser.LOAN_STATE_WEIDAIKUAN) {
						yhUser.setLoanState(YhUser.LOAN_STATE_ZHENGCHANG);
						yhUser.setYhUserId(loan.getTyUserId());
						yhUserService.updateLoanSate(yhUser);
					}
					loanBasicService.add(loanBasic);
					json.setSuccess(true);
					json.setMsg("已完成借款");
				}
			}

			// 月息
		} else if (loan.getLoanType() == Loan.TYPE_MOUTH) {
			// 借款月数
			String start = DateUtil.toStringXiumaDateTime(loan.getStartTime());
			String stop = DateUtil.toStringXiumaDateTime(loan.getStopTime());
			Integer strYear = Integer.parseInt(start.substring(0, 4));// 拿到开始时间年份
			Integer strMouth = Integer.parseInt(start.substring(4, 6));// 拿到开始时间月份
			Integer strDay = Integer.parseInt(start.substring(6, 8));// 拿到开始时间天数
			Integer stoYear = Integer.parseInt(stop.substring(0, 4));// 拿到结束时间年份
			Integer stomouth = Integer.parseInt(stop.substring(4, 6));// 拿到开始时间月份
			Integer stoDay = Integer.parseInt(stop.substring(6, 8));// 拿到开始时间天数
			int mouth = ((stoYear - strYear) * 12) + stomouth - strMouth;
			if (stoDay > strDay) {
				mouth += 1;
			}

			// 实际到手金额
			double ad = loan.getLoanMoney() - loan.getHomeVisitsMoney()
					- loan.getIntermediary() - loan.getBond();
			loan.setActualMoney(ad);

			// 实际还款金额
			double sm = loan.getLoanMoney()
					+ (loan.getLoanMoney() * mouth * interest / 100);
			loan.setStillMoney(sm);

			// 实际收款金额
			double cm = sm - loan.getBond();
			loan.setCollectMoney(cm);

			Date t = new Date();
			String tt = DateUtil.toStringXiumaDateTime(t);
			loan.setLoanType(Loan.TYPE_MOUTH);
			loan.setUserName(user.getName());
			loan.setUserId(user.getUserId());
			loan.setProductName("先息后本");
			loan.setLoanId(tt);
			loan.setCreateTime(new Date());
			loan.setAlreadyRepaid(0);
			loan.setNotYet(loan.getStillMoney());
			loanService.add(loan);
			finance.setFinanceId(tt);
			finance.setYhUserName(loan.getTyUserName());
			finance.setYhUserId(loan.getTyUserId());
			finance.setUserId(user.getUserId());
			finance.setUserName(user.getName());
			finance.setLoanMoney(loan.getLoanMoney());
			finance.setContractMoney(loan.getContractMoney());
			finance.setStartTime(loan.getStartTime());
			finance.setStopTime(loan.getStopTime());
			finance.setHomeVisitsMoney(loan.getHomeVisitsMoney());
			finance.setIntermediary(loan.getIntermediary());
			finance.setBond(loan.getBond());
			finance.setActualMoney(loan.getActualMoney());
			finance.setStillMoney(loan.getStillMoney());
			finance.setCollectMoney(loan.getCollectMoney());
			loan.setCreateTime(new Date());
			financeService.insert(finance);

			// 每期还款金额
			double oneMoney = sm / mouth;
			BigDecimal b = new BigDecimal(oneMoney);
			double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			for (int i = 1; i < mouth + 1; i++) {

				// 每期还款时间
				SimpleDateFormat dateFormat = new SimpleDateFormat(
						"yyyy年MM月dd日");
				Date date = loan.getStartTime();
				Date date1 = loan.getStopTime();
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(loan.getStartTime()); // 设置为当前时间
				calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + i);
				date = calendar.getTime();
				if (date.getTime() < date1.getTime()) {
					System.out.println("下一个月的还款时间： " + dateFormat.format(date)
							+ "第" + i + "期还款，还款金额为" + f1);

					loanBasic.setCreateTime(new Date());
					loanBasic.setLoanBasicId(tt + i);
					loanBasic.setLoanId(loan.getLoanId());
					loanBasic.setStillMoney(f1);
					loanBasic.setStillState(LoanBasic.STILL_STATE_WEIHUAN);
					loanBasic.setStillTime(date);
					loanBasic.setUserId(user.getUserId());
					loanBasic.setYhUserId(loan.getTyUserId());
					loanBasic.setUserName(user.getName());
					YhUser y = yhUserService.selectByPrimaryKey(loan
							.getTyUserId());
					if (y != null
							&& y.getLoanState() == YhUser.LOAN_STATE_WEIDAIKUAN) {
						yhUser.setLoanState(YhUser.LOAN_STATE_ZHENGCHANG);
						yhUser.setYhUserId(loan.getTyUserId());
						yhUserService.updateLoanSate(yhUser);
					}
					loanBasicService.add(loanBasic);

					json.setSuccess(true);
					json.setMsg("已完成借款");
				} else if (date.getTime() > date1.getTime()) {
					System.out.println("下一个月的还款时间： " + dateFormat.format(date1)
							+ "第" + i + "期还款，还款金额为" + f1);

					loanBasic.setCreateTime(new Date());
					loanBasic.setLoanBasicId(tt + i);
					loanBasic.setLoanId(loan.getLoanId());
					loanBasic.setStillMoney(f1);
					loanBasic.setStillState(LoanBasic.STILL_STATE_WEIHUAN);
					loanBasic.setStillTime(date1);
					loanBasic.setUserId(user.getUserId());
					loanBasic.setUserName(user.getName());
					loanBasic.setYhUserId(loan.getTyUserId());
					YhUser y = yhUserService.selectByPrimaryKey(loan
							.getTyUserId());
					if (y != null
							&& y.getLoanState() == YhUser.LOAN_STATE_WEIDAIKUAN) {
						yhUser.setLoanState(YhUser.LOAN_STATE_ZHENGCHANG);
						yhUser.setYhUserId(loan.getTyUserId());
						yhUserService.updateLoanSate(yhUser);
					}
					loanBasicService.add(loanBasic);
					json.setSuccess(true);
					json.setMsg("已完成借款");
				}

			}

		}
		return json;
	}

	/**
	 * 等额本息
	 * 
	 * @param loan
	 * @return
	 */
	@ResponseBody
	@RequestMapping("ajax_add_loan_debx")
	public JsonVo<String> ajax_add_loan_debx(Loan loan, LoanBasic loanBasic,
			String str, String sto, double interest, YhUser yhUser)
			throws ParseException {
		User user = UserManagerUtil.getCurrentUser();
		loan.setStartTime(DateUtil.toDate(str));
		loan.setStopTime(DateUtil.toDate(sto));
		JsonVo<String> json = new JsonVo<String>();
		// 先息后本 日息
		if (loan.getLoanType() == Loan.TYPE_DAY) {
			// 借款天数
			long time = loan.getStopTime().getTime()
					- loan.getStartTime().getTime();
			long day = time / 1000 / 60 / 60 / 24;

			// 实际到手金额
			double ad = loan.getLoanMoney() - loan.getHomeVisitsMoney()
					- loan.getIntermediary() - loan.getBond();
			loan.setActualMoney(ad);

			// 实际还款金额
			double sm = ((loan.getLoanMoney() / day) + (loan.getLoanMoney()
					* interest / 100))
					* day;
			loan.setStillMoney(sm);

			// 实际收款金额
			double cm = sm - loan.getBond();
			loan.setCollectMoney(cm);

			Date t = new Date();
			String tt = DateUtil.toStringXiumaDateTime(t);
			loan.setLoanType(Loan.TYPE_DAY);
			loan.setUserName(user.getName());
			loan.setUserId(user.getUserId());
			loan.setProductName("等额本息");
			loan.setLoanId(tt);
			loan.setCreateTime(new Date());
			loan.setAlreadyRepaid(0);
			loan.setNotYet(loan.getStillMoney());
			loanService.add(loan);

			// 每期还款金额
			double oneMoney = sm / day;
			BigDecimal b = new BigDecimal(oneMoney);
			double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			for (int i = 1; i < day + 1; i++) {
				// 每期还款时间
				SimpleDateFormat dateFormat = new SimpleDateFormat(
						"yyyy年MM月dd日");
				Date date = loan.getStartTime();
				Date date1 = loan.getStopTime();
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				calendar.add(Calendar.DAY_OF_MONTH, +1 * i);
				date = calendar.getTime();
				if (date.getTime() < date1.getTime()) {
					System.out.println("下一次的还款时间： " + dateFormat.format(date)
							+ "第" + i + "期还款，还款金额为" + f1);
					loanBasic.setCreateTime(new Date());
					loanBasic.setLoanBasicId(tt + i);
					loanBasic.setLoanId(loan.getLoanId());
					loanBasic.setStillMoney(f1);
					loanBasic.setStillState(LoanBasic.STILL_STATE_WEIHUAN);
					loanBasic.setStillTime(date);
					loanBasic.setUserId(user.getUserId());
					loanBasic.setYhUserId(loan.getTyUserId());
					yhUser.setLoanState(YhUser.LOAN_STATE_ZHENGCHANG);
					yhUserService.updateByPrimaryKeySelective(yhUser);
					loanBasicService.add(loanBasic);
					json.setSuccess(true);
					json.setMsg("已完成借款");
				} else if (date.getTime() >= date1.getTime()) {
					System.out.println("下一次的还款时间： " + dateFormat.format(date1)
							+ "第" + i + "期还款，还款金额为" + f1);
					loanBasic.setCreateTime(new Date());
					loanBasic.setLoanBasicId(tt + i);
					loanBasic.setLoanId(loan.getLoanId());
					loanBasic.setStillMoney(f1);
					loanBasic.setStillState(LoanBasic.STILL_STATE_WEIHUAN);
					loanBasic.setStillTime(date1);
					loanBasic.setUserId(user.getUserId());
					loanBasic.setYhUserId(loan.getTyUserId());
					yhUser.setLoanState(YhUser.LOAN_STATE_ZHENGCHANG);
					yhUserService.updateByPrimaryKeySelective(yhUser);
					loanBasicService.add(loanBasic);
					json.setSuccess(true);
					json.setMsg("已完成借款");
				}
			}

			// 周息
		} else if (loan.getLoanType() == Loan.TYPE_ZHOU) {
			// 借款星期
			long time = loan.getStopTime().getTime()
					- loan.getStartTime().getTime();
			long day = time / 1000 / 60 / 60 / 24;
			int a = (int) (day / 7);
			double aa = (double) a;
			double b = (double) day / 7;
			int week = 0;
			if (aa == b) {
				week = a;
			} else if (aa < b) {
				week = a + 1;
			}

			// 实际到手金额
			double ad = loan.getLoanMoney() - loan.getHomeVisitsMoney()
					- loan.getIntermediary() - loan.getBond();
			loan.setActualMoney(ad);

			// 实际还款金额
			double sm = ((loan.getLoanMoney() / week) + (loan.getLoanMoney()
					* (interest / 100) * 7))
					* week;
			loan.setStillMoney(sm);

			// 实际收款金额
			double cm = sm - loan.getBond();
			loan.setCollectMoney(cm);

			Date t = new Date();
			String tt = DateUtil.toStringXiumaDateTime(t);
			loan.setLoanType(Loan.TYPE_ZHOU);
			loan.setUserName(user.getName());
			loan.setUserId(user.getUserId());
			loan.setProductName("等额本息");
			loan.setLoanId(tt);
			loan.setCreateTime(new Date());
			loan.setAlreadyRepaid(0);
			loan.setNotYet(loan.getStillMoney());
			loanService.add(loan);

			// 每期还款金额
			double oneMoney = sm / week;
			BigDecimal bb = new BigDecimal(oneMoney);
			double f1 = bb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			for (int i = 1; i < week + 1; i++) {
				// 每期还款时间
				SimpleDateFormat dateFormat = new SimpleDateFormat(
						"yyyy年MM月dd日");
				Date date = loan.getStartTime();
				Date date1 = loan.getStopTime();
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				calendar.add(Calendar.DAY_OF_MONTH, +7 * i);
				date = calendar.getTime();
				if (date.getTime() < date1.getTime()) {
					System.out.println("下一个星期的还款时间： " + dateFormat.format(date)
							+ "第" + i + "期还款，还款金额为" + f1);

					loanBasic.setCreateTime(new Date());
					loanBasic.setLoanBasicId(tt + i);
					loanBasic.setLoanId(loan.getLoanId());
					loanBasic.setStillMoney(f1);
					loanBasic.setStillState(LoanBasic.STILL_STATE_WEIHUAN);
					loanBasic.setStillTime(date);
					loanBasic.setUserId(user.getUserId());
					loanBasic.setYhUserId(loan.getTyUserId());
					yhUser.setLoanState(YhUser.LOAN_STATE_ZHENGCHANG);
					yhUserService.updateByPrimaryKeySelective(yhUser);
					loanBasicService.add(loanBasic);
					json.setSuccess(true);
					json.setMsg("已完成借款");
				} else if (date.getTime() > date1.getTime()) {
					System.out.println("下一个星期的还款时间： "
							+ dateFormat.format(date1) + "第" + i + "期还款，还款金额为"
							+ f1);

					loanBasic.setCreateTime(new Date());
					loanBasic.setLoanBasicId(tt + i);
					loanBasic.setLoanId(loan.getLoanId());
					loanBasic.setStillMoney(f1);
					loanBasic.setStillState(LoanBasic.STILL_STATE_WEIHUAN);
					loanBasic.setStillTime(date1);
					loanBasic.setUserId(user.getUserId());
					loanBasic.setYhUserId(loan.getTyUserId());
					yhUser.setLoanState(YhUser.LOAN_STATE_ZHENGCHANG);
					yhUserService.updateByPrimaryKeySelective(yhUser);
					loanBasicService.add(loanBasic);
					json.setSuccess(true);
					json.setMsg("已完成借款");
				}
			}

		}
		return json;
	}

	/*
	 * 客户还款
	 */
	@RequestMapping("money.htm")
	public String money(Model model, String yhUserId) {
		YhUser user = yhUserService.selectByPrimaryKey(yhUserId);
		model.addAttribute("user", user);

		return "manage/user/money";
	}
	
	/*
	 * 客户还款
	 */
	@RequestMapping("money_info.htm")
	public String money_info(Model model, String yhUserIdcard,String yhUserId,String loanId) {
		User u = UserManagerUtil.getCurrentUser();
		YhUser card = yhUserService.findByIdCard(yhUserIdcard);
		model.addAttribute("card", card);
		YhUser user = yhUserService.selectByPrimaryKey(yhUserId);
		model.addAttribute("user", user);

		// 查询未还款贷款数据
		List<LoanBasic> list = loanBasicService.loanBasicListMoney(
				user.getYhUserId(), user.getUserId(), 0,loanId);
		int count = list.size();
		for (int i = 0; i < list.size(); i++) {
			int a = i + 1;
			list.get(i).setP("p" + a);
			list.get(i).setSumbitState(a);
		}
		List<LoanBasic> list1 = loanBasicService.loanBasicListMoney(
				user.getYhUserId(), user.getUserId(), 1,loanId);
		for (int i = 0; i < list1.size(); i++) {
			list1.get(i).setAt(
					DateUtil.Date2Stirng2Second(list1.get(i).getAcyualTime()));
		}

		// 查询已还款贷款数据
		model.addAttribute("list", list);
		model.addAttribute("list1", list1);
		model.addAttribute("count", count);
		model.addAttribute("u", u);
		return "manage/user/money_info";
	}
	

	@ResponseBody
	@RequestMapping("ajax_user_loan_list")
	public Map<String, Object> ajax_user_loan_list(String yhUserId) {
		List<Loan> list = loanService.loanList(yhUserId);
		Map<String, Object> map = new HashMap<>();
		map.put("code", 0);
		map.put("msg", 0);
		map.put("count", 1000);
		map.put("data", list);
		return map;
	}

	/**
	 * 逾期天数失焦
	 */
	@ResponseBody
	@RequestMapping("ajax_user_over_day")
	public JsonVo<Loan> ajax_user_over_day(Integer day, String tyUserId) {
		JsonVo<Loan> json = new JsonVo<Loan>();
		String a = "先息后本";
		String b = "等额本息";
		Loan loan = loanService.findById(tyUserId);
		if (loan == null) {
			return json;
		} else {
			if (loan.getLoanType() == Loan.TYPE_DAY
					&& StringUtils.equals(loan.getProductName(), a)) {
				double dayMoney = loan.getContractMoney() * 0.05 * day;
				loan.setDayMoney(dayMoney);
			} else if (loan.getLoanType() == Loan.TYPE_MOUTH
					&& StringUtils.equals(loan.getProductName(), a)) {
				double dayMoney = loan.getContractMoney() * 0.05 * day;
				loan.setDayMoney(dayMoney);
			} else if (loan.getLoanType() == Loan.TYPE_DAY
					&& StringUtils.equals(loan.getProductName(), b)) {
				double dayMoney = loan.getContractMoney() * 0.05 * day;
				loan.setDayMoney(dayMoney);
			} else if (loan.getLoanType() == Loan.TYPE_ZHOU
					&& StringUtils.equals(loan.getProductName(), b)) {
				double dayMoney = loan.getContractMoney() * 0.05 * day;
				loan.setDayMoney(dayMoney);
			}
			json.setSuccess(true);
			json.setT(loan);
			return json;
		}

	}

	/**
	 * 多图片上传
	 */
	@ResponseBody
	@RequestMapping(value = "photo.json", method = RequestMethod.POST)
	public JsonVo<String> handleRequest(
			@RequestParam("file") MultipartFile file, HttpServletRequest request)
			throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		/**
		 * 
		 * 上传
		 */
		if (FileUtils.checkPhotoFile(file)) {
			String url = FileUtils.saveFile(file, request);
			json.setT(url);
			json.setSuccess(true);
			json.check();
		} else {
			String errorMessage = "上传的文件只能是jpg,png,gif的图片格式";
			json.setMsg(errorMessage);
			json.setSuccess(false);
		}
		return json;
	}

	/**
	 * 还款
	 */
	@ResponseBody
	@RequestMapping("ajax_update_money")
	public JsonVo<String> ajax_update_money(YhUser record,LoanBasic loanBasic, Loan loan) {
		JsonVo<String> json = new JsonVo<String>();
		LoanBasic  l = loanBasicService.findById(loanBasic.getLoanBasicId());
		if(loanBasic.getOverDay() != 0){
			record.setLoanState(3);
			record.setYhUserId(l.getYhUserId());
			yhUserService.updateByPrimaryKeySelective(record);
		}else{
			record.setLoanState(1);
			record.setYhUserId(l.getYhUserId());
			yhUserService.updateByPrimaryKeySelective(record);
		}
		loanBasic.setStillState(1);
		loanBasic.setAcyualTime(new Date());
		loanBasic.setDifferMoney(loanBasic.getShouldMoney()
				- loanBasic.getAcyualMoney());
		loan.setAlreadyRepaid(loanBasic.getAcyualMoney());
		loan.setNotYet(loan.getNotYet() - loan.getAlreadyRepaid()
				+ loanBasic.getOverMoney());
		loan.setLoanId(loanBasic.getLoanId());
		loanBasicService.update(loanBasic);
		loanService.update(loan);
		json.setSuccess(true);
		return json;
	}
	
	
	/**
	 * 查询每一条贷款还款list
	 */
	@ResponseBody
	@RequestMapping("ajax_loan_loanBasic_list")
	public JsonVo<List<LoanBasic>> ajax_loan_loanBasic_list(String loanId, String yhUserId,
			Integer state){
		JsonVo<List<LoanBasic>> json = new JsonVo<List<LoanBasic>>();
		
		//未还款
		List<LoanBasic> list = loanBasicService.findByLoanIdUserId(loanId, yhUserId, 0);
		for (int i = 0; i < list.size(); i++) {
			int a = i + 1;
			list.get(i).setP("p" + a);
			list.get(i).setSumbitState(a);
		}
		
		json.setSuccess(true);
		json.setT(list);
		return json;
	}
	
	@Resource(name = "TaobaoConsumptionService")
	private TaobaoConsumptionService taobaoConsumptionService;
	
	@Resource(name = "JingdongConsumptionService")
	private JingdongConsumptionService jingdongConsumptionService;
	
	@Resource(name = "CallRecordInfoService")
	private CallRecordInfoService callRecordInfoService;
	
	@ResponseBody
	@RequestMapping("taobao_list")
	public Map<String, Object> taobao_list(String kfUserId){
		Map<String, Object> map = new HashMap<>();
		List<TaobaoConsumption> list = taobaoConsumptionService.findByList(kfUserId);
		map.put("code",0);
		map.put("msg", "成功");
		map.put("count", list.size());
		map.put("data", list);
		return map;
	}
	
	@ResponseBody
	@RequestMapping("jingdongtaobao_list")
	public Map<String, Object> jingdong(String kfUserId){
		List<JingdongConsumption> list = jingdongConsumptionService.findByList(kfUserId);
		Map<String, Object> map = new HashMap<>();
		map.put("code",0);
		map.put("msg", "成功");
		map.put("count", list.size());
		map.put("data", list);
		return map;
	}
	
	@ResponseBody
	@RequestMapping("call_list")
	public Map<String, Object> CallRecordInfo(String kfUserId){
		Map<String, Object> map = new HashMap<>();
		List<CallRecordInfo> list = callRecordInfoService.findByList(kfUserId);
		map.put("code",0);
		map.put("msg", "成功");
		map.put("count", list.size());
		map.put("data", list);
		return map;
	}
	
}
